/**
 * Created by Buynow on 2017/8/18.
 */


var tagManager = {
    initTable:function () {
        $('.tag-manager-table').bootstrapTable({

            columns: [{
                field: 'state',
                checkbox: true,
            }, {
                field: 'name',
                title: '类别名称',
                editable:true,

            }, {
                field: 'count',
                title: '数量'
            }],
            url: '/admin/articleTags',
            onEditableSave: function (field, row, oldValue, $el) {
                if (field == 'name'){
                    obj = {
                        name:row.name,
                    }
                    $.ajax({
                        type: "post",
                        url: "/admin/articleTag/"+row.name+"?_method=PUT",
                        contentType:"application/json",
                        dataType:'json',
                        data: JSON.stringify(obj),
                        success: function (data) {
                            if (data.status == 200) {
                                alert("修改成功");
                            }
                        },
                        error: function () {
                            alert("Error");
                        },
                        complete: function () {

                        }

                    });
                }


            }

        });
    },
    refreshTable:function () {
        $('.tag-manager-table').bootstrapTable('refresh');
    },
    deleteByIdArr:function (idArr) {
        tagArr = [];
        for (i=0;i<idArr.length;i++){
            tagArr.push({name: idArr[i]});
        }
        $.post({
            type: "post",
            url: "/admin/articleTag?_method=DELETE",
            contentType:"application/json",
            dataType:'json',
            data: JSON.stringify(tagArr),
            success:function (data) {
                if (data.stauts == 200) {
                    index.alert('success', '删除成功 !');
                    tagManager.refreshTable();
                }else {
                    index.alert('danger', '删除失败 !');
                }
            },
            complete: function () {
                tagManager.refreshTable();
            }
        })
    },
    addTag:function () {
        name = $('input[name="tag-name"]').val();
        if (name){
            $.ajax({
                url:'admin/articleTag',
                type:'post',
                data:{
                    name:name
                },
                success:function (data) {
                    console.log(data)
                    if (data.stauts == 200) {
                        index.alert('success', '添加成功 !');
                        tagManager.refreshTable();
                    }else {
                        index.alert('danger', '添加失败 !');
                    }
                }
            })
        }
    }
}

$(function () {
    tagManager.initTable();
    $('.btn-refresh').click(function () {
        tagManager.refreshTable();
    })
    $('.btn-tag-add').click(function () {
        tagManager.addTag();
    })
    $('.btn-delete-tags').click(function () {
        tagManager.deleteByIdArr(index.getAllSelectedRowId());
    })
})
